﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PhoneStoreWeb.Models;
using System.Web.Script.Serialization;
using PagedList;

namespace PhoneStoreWeb.Areas.Admin.Controllers
{
    public class ProductController : Controller
    {
        private PhoneStoreEntities db = new PhoneStoreEntities();

        //
        // GET: /Admin/Product/

        public ActionResult Index()
        {
            var products = db.Products.Include(p => p.Category).Include(p => p.Manufacturer).Include(p => p.PhoneTablet).OrderBy(p => p.Name);
            
            
            return View(products.ToList());
        }

        //
        // GET: /Admin/Product/Details/5

        public ActionResult Details(int id = 0)
        {
            Product product = db.Products.Find(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            return View(product);
        }

        //
        // GET: /Admin/Product/Create

        public ActionResult Create()
        {
            ViewBag.GetLastProductID = db.Products.OrderByDescending(p => p.ProductID).FirstOrDefault().ProductID;
            ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "Name");
            ViewBag.ManufacturerID = new SelectList(db.Manufacturers, "ManufacturerID", "Name");
            ViewBag.ProductID = new SelectList(db.PhoneTablets, "PhoneTabletID", "PhoneTabletID");          
            return View();
        }

        //
        // POST: /Admin/Product/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Product product)
        {
            if (ModelState.IsValid)
            {
                db.Products.Add(product);
                db.SaveChanges();

                PhoneTablet pt = new PhoneTablet();
                pt.PhoneTabletID = product.ProductID;
                db.PhoneTablets.Add(pt);
                db.SaveChanges();

                return RedirectToAction("CreateDetails", new { id=pt.PhoneTabletID});
            }

            ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "Name", product.CategoryID);
            ViewBag.ManufacturerID = new SelectList(db.Manufacturers, "ManufacturerID", "Name", product.ManufacturerID);
            ViewBag.ProductID = new SelectList(db.PhoneTablets, "PhoneTabletID", "PhoneTabletID", product.ProductID);
            return View(product);
        }

        public ActionResult CreateDetails(int id)
        {
            PhoneTablet phonetablet = db.PhoneTablets.Find(id);
            if (phonetablet == null)
            {
                return HttpNotFound();
            }
            ViewBag.GeneralID = new SelectList(db.Phone_General, "GeneralID", "GeneralID");
            ViewBag.BodyID = new SelectList(db.Phone_Body, "BodyID", "BodyID");
            ViewBag.DisplayID = new SelectList(db.Phone_Display, "DisplayID", "DisplayID");
            ViewBag.SoundID = new SelectList(db.Phone_Sound, "SoundID", "SoundID");
            ViewBag.MemoryID = new SelectList(db.Phone_Memory, "MemoryID", "MemoryID");
            ViewBag.DataID = new SelectList(db.Phone_Data, "DataID", "DataID");
            ViewBag.CameraID = new SelectList(db.Phone_Camera, "CameraID", "CameraID");
            ViewBag.FeaturesID = new SelectList(db.Phone_Features, "FeaturesID", "FeaturesID");
            ViewBag.BatteryID = new SelectList(db.Phone_Battery, "BatteryID", "BatteryID");
            ViewBag.MiscID = new SelectList(db.Phone_Misc, "MiscID", "MiscID");
            ViewBag.TestsID = new SelectList(db.Phone_Tests, "TestsID", "TestsID");
            
            return View(phonetablet);
        }

        [HttpPost]
        public ActionResult CreateDetails(PhoneTablet phonetablet)
        {
            if (ModelState.IsValid)
            {
                db.Entry(phonetablet).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
                
            }
            ViewBag.GeneralID = new SelectList(db.Phone_General, "GeneralID", "GeneralID");
            ViewBag.BodyID = new SelectList(db.Phone_Body, "BodyID", "BodyID");
            ViewBag.DisplayID = new SelectList(db.Phone_Display, "DisplayID", "DisplayID");
            ViewBag.SoundID = new SelectList(db.Phone_Sound, "SoundID", "SoundID");
            ViewBag.MemoryID = new SelectList(db.Phone_Memory, "MemoryID", "MemoryID");
            ViewBag.DataID = new SelectList(db.Phone_Data, "DataID", "DataID");
            ViewBag.CameraID = new SelectList(db.Phone_Camera, "CameraID", "CameraID");
            ViewBag.FeaturesID = new SelectList(db.Phone_Features, "FeaturesID", "FeaturesID");
            ViewBag.BatteryID = new SelectList(db.Phone_Battery, "BatteryID", "BatteryID");
            ViewBag.MiscID = new SelectList(db.Phone_Misc, "MiscID", "MiscID");
            ViewBag.TestsID = new SelectList(db.Phone_Tests, "TestsID", "TestsID");
            return View(phonetablet);
        }

        public ActionResult CreateSuccess()
        {
            return View();
        }

        public string GetPhoneGeneral(int id)
        {
            var generals = (from q in db.Phone_General
                           where q.GeneralID == id
                           select new { 
                               q.TwoGNetwork,
                               q.ThreeGNetwork,
                               q.FourGNetwork,
                               q.Sim,
                               q.Announced,
                               q.Status
                           }).ToArray();
            return new JavaScriptSerializer().Serialize(generals);
        }

        public string GetPhoneBody(int id)
        {
            var bodys = (from q in db.Phone_Body
                         where q.BodyID == id
                         select new {
                            q.Dimensions,
                            q.Weight
                         }).ToArray();
            return new JavaScriptSerializer().Serialize(bodys);
        }

        public string GetPhoneDisplay(int id)
        {
            var displays = (from q in db.Phone_Display
                            where q.DisplayID == id
                            select new
                            {
                                q.Type,
                                q.Size,
                                q.Multitouch,
                                q.Protection
                            }).ToArray();
            return new JavaScriptSerializer().Serialize(displays);
        }

        public string GetPhoneSound(int id)
        {
            var sounds = (from q in db.Phone_Sound
                          where q.SoundID == id
                          select new
                          {
                              q.AlertTypes,
                              q.LoudSpeaker,
                              q.ThreePointFiveMMJack
                          }).ToArray();
            return new JavaScriptSerializer().Serialize(sounds);
        }

        public string GetPhoneData(int id)
        {
            var datas = (from q in db.Phone_Data
                         where q.DataID == id
                         select new
                         {
                             q.GPRS,
                             q.EDGE,
                             q.Speed,
                             q.WLAN,
                             q.Bluetooth,
                             q.NFC,
                             q.InfraredPort,
                             q.USB
                         }).ToArray();
            return new JavaScriptSerializer().Serialize(datas);
        }

        public string GetPhoneCamera(int id)
        {
            var cameras = (from q in db.Phone_Camera
                           where q.CameraID == id
                           select new
                           {
                               q.PrimaryCamera,
                               q.Features,
                               q.Video,
                               q.SecondaryCamera
                           }).ToArray();
            return new JavaScriptSerializer().Serialize(cameras);
        }

        public string GetPhoneFeatures(int id)
        {
            var features = (from q in db.Phone_Features
                            where q.FeaturesID == id
                            select new
                            {
                                q.OS,
                                q.Chipset,
                                q.CPU,
                                q.GPU,
                                q.Sensors,
                                q.Messaging,
                                q.Browser,
                                q.Radio,
                                q.GPS,
                                q.Java,
                                q.Colors,
                                q.Others
                            }).ToArray();
            return new JavaScriptSerializer().Serialize(features);
        }

        public string GetPhoneBattery(int id)
        {
            var batterys = (from q in db.Phone_Battery
                            where q.BatteryID == id
                            select new
                            {
                                q.Model,
                                q.StandBy,
                                q.TalkTime,
                                q.MusicPlay
                            }).ToArray();

            return new JavaScriptSerializer().Serialize(batterys);
        }

        public string GetPhoneMisc(int id)
        {
            var miscs = (from q in db.Phone_Misc
                         where q.MiscID == id
                         select new
                         {
                             q.SAR_US,
                             q.SAR_EU,
                             q.PriceGroup
                         }).ToArray();
            return new JavaScriptSerializer().Serialize(miscs);
        }

        public string GetPhoneTests(int id)
        {
            var tests = (from q in db.Phone_Tests
                         where q.TestsID == id
                         select new
                         {
                             q.Display,
                             q.LoudSpeaker,
                             q.AudioQuality,
                             q.Camera
                         }).ToArray();
            return new JavaScriptSerializer().Serialize(tests);
        }

        public string GetPhoneMemory(int id)
        {
            var memory = (from q in db.Phone_Memory
                          where q.MemoryID == id
                          select new
                          {
                              q.CardSlot,
                              q.Internal
                          }).ToArray();

            return new JavaScriptSerializer().Serialize(memory);
        }

        
       

        //
        // GET: /Admin/Product/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Product product = db.Products.Find(id);
            PhoneTablet phonetablet = db.PhoneTablets.Find(id);
            var tuple = new Tuple<Product, PhoneTablet>(product, phonetablet);
            if (product == null)
            {
                return HttpNotFound();
            }
            ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "Name", product.CategoryID);
            ViewBag.ManufacturerID = new SelectList(db.Manufacturers, "ManufacturerID", "Name", product.ManufacturerID);
            ViewBag.ProductID = new SelectList(db.PhoneTablets, "PhoneTabletID", "PhoneTabletID", product.ProductID);
            return View(tuple);
        }

        //
        // POST: /Admin/Product/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Product product)
        {
            if (ModelState.IsValid)
            {
                db.Entry(product).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "Name", product.CategoryID);
            ViewBag.ManufacturerID = new SelectList(db.Manufacturers, "ManufacturerID", "Name", product.ManufacturerID);
            ViewBag.ProductID = new SelectList(db.PhoneTablets, "PhoneTabletID", "PhoneTabletID", product.ProductID);
            return View(product);
        }

        //
        // GET: /Admin/Product/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Product product = db.Products.Find(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            return View(product);
        }

        //
        // POST: /Admin/Product/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Product product = db.Products.Find(id);
            db.Products.Remove(product);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}